#! /bin/sh
# PCP QA Test No. 107
# Check pmlc "query" command against the primary pmlogger
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_filter()
{
    # Note
    #	... already using all instances ... line comes from dynamic PMNS
    #	support where sample.secret.foo.bar.max.redirect and
    #   sampledso.secret.foo.bar.max.redirect are aliases (i.e. have the
    #   same PMID) for pmcd.agent.status ... so the Warning is expected
    #
    echo "Checking for errors in pmlc output ..."
    sed <$1 \
	-e '/Warning: already using all instances for pmcd.agent.status/d' \
	-e '/^$/d' \
	-e '/^disk[^:]*$/d' \
	-e '/^network[^:]*$/d' \
	-e '/^kernel[^:]*$/d' \
	-e '/^pmcd[^:]*$/d' \
	-e '/^sample[^:]*$/d' \
	-e '/^sampledso[^:]*$/d' \
	-e '/^    mand on /d' \
	-e '/^    mand off /d' \
	-e '/^    adv  off /d' \
	-e '/^    adv  on /d' \
	-e '/^    (no instances)/d' \
	-e '=' \
	-e 's/^/	/' \
    | sed -e '/^[0-9][0-9]*$/s//line &:/'
}

trap "rm -f $tmp.*; exit" 0 1 2 3 15

# don't need to regenerate config.default with pmlogconf
#
export PMLOGGER_CHECK_SKIP_LOGCONF=yes

# Need healthy pmlogger connected to pmcd ... force this
#
if ! _service pmcd restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmcd || _exit 1
if ! _service pmlogger restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmlogger || _exit 1
sleep 5

# real QA test starts here
pmlc <<End-of-File >>$tmp.tmp 2>&1
connect primary
query { disk network kernel pmcd sample sampledso }
End-of-File

cat $tmp.tmp >>$seq_full

$PCP_ECHO_PROG $PCP_ECHO_N "Expect at least one metric with logging state \"on\" ...""$PCP_ECHO_C"
if grep -E '^    (mand on )|(adv  on )' $tmp.tmp >/dev/null
then
    echo " PASS"
else
    echo " FAIL"
fi

$PCP_ECHO_PROG $PCP_ECHO_N "Expect at least 250 metrics to be reported ...""$PCP_ECHO_C"
num=`grep -E '^(disk|network|kernel|pmcd|sample|sampledso)\.' $tmp.tmp | wc -l | sed -e 's/ //g'`
if [ $num -ge 250 ]
then
    echo " PASS"
else
    echo " FAIL (found $num)"
fi

_filter $tmp.tmp
